declare namespace xpertstats="http://www.w3.org/1999/xhtml";
declare namespace mapping="http://rpmestre.net/xcrawler";

declare function xpertstats:contains-any-of  ( $arg as xs:string? ,     $searchStrings as xs:string* )  as xs:boolean 
  {
   some $searchString in $searchStrings
   satisfies contains($arg,$searchString)
  } ;

<betfairodds refreshTime="{current-dateTime()}">
{
let $teams := doc("mapping.xml")/mapping:mapping/mapping:team_names/mapping:team/mapping:bf/text()
for $event in  doc("BF.xml")/betfair/event
order by $event/@date descending
	return  
		if (xpertstats:contains-any-of ($event/@name, $teams))  then
		( 
			<event>
				<name>{data($event/@name)}</name>
				<date>{data($event/@date)}</date>		
				<markets>	
					{ 
						for $subevent in $event/subevent
							return	<market id="{data($subevent/@id)}">
											<title>{data($subevent/@title)}</title>
											<totalAmountMatched>{data($subevent/@TotalAmountMatched)}</totalAmountMatched>
											<runners>
											{
												for $selection in $subevent/selection
													return	<selection id="{data($selection/@id)}">
																<name>{data($selection/@name)}</name>
																<back>
																	<price>{data($selection/@backp1)}</price>
																	<odd>{data($selection/@backs1)}</odd>
																</back>
																<lay>
																	<price>{data($selection/@layp1)}</price>
																	<odd>{data($selection/@lays1)}</odd>
																</lay>
															</selection>
											}
											</runners>
										</market>
						
					}
				</markets>	
			</event> 
		)
		else ()
}
</betfairodds>